package gold.digger;

import java.util.*;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC754 {
    public long startExecuteTime = System.currentTimeMillis();

    /*
     * @param 此题目直接用别人代码，自己只理解思想
     * 经典解法，每日一看
     * 数学题，思路非常清奇
     * @return:
     */
    class Solution {
        Map<Integer, Integer> memo = new TreeMap<>();
        int stepLimit = 24;

        public void getExample() {
            dfs(0, 0);
            for (Integer num : memo.keySet()) {
                System.out.println(num + " min step: " + memo.get(num));
            }
        }

        public int reachNumber(int target) {
            if (target < 0) return reachNumber(-target);
            int i = 1;
            while (true) {
                int s = (i * i + i) / 2;
                if (s >= target && (s - target) % 2 == 0) return i;
                i += 1;
            }
        }

        public void dfs(int cur, int step) {
            if (step > stepLimit) return;
            memo.put(cur, Math.min(memo.getOrDefault(cur, Integer.MAX_VALUE), step));
            dfs(cur + step + 1, step + 1);
            dfs(cur - step - 1, step + 1);
        }

    }

    public void run() {
        Solution solution = new Solution();
        List<Integer> list = new ArrayList<>();
        System.out.println(solution.reachNumber(10));
    }

    public static void main(String[] args) throws Exception {
        LC754 an = new LC754();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
